package com.jlu155.CourseManager.repository;

import java.util.List;

import com.jlu155.CourseManager.entity.*;
import org.apache.ibatis.annotations.*;

public interface StudentCourseRepository {

    // 插入学生课程信息
    @Insert("insert into student_course(student_id, course_id, grade, tc_id) values (#{student_id}, #{course_id}, #{grade}, #{tc_id})")
    public int insertStudentCourse(StudentCourse studentCourse);

    // 按学生ID查询课程信息
    @Select("select * from student_course where student_id=#{student_id}")
    List<StudentCourse> findStudentCourseByStudentId(Integer student_id);

    // 按学生ID查询课程信息，分页         LIMIT #{offset}, #{limit}
    @Select("<script>" +
            "select * from student_course " +
            "where 1=1 " +
            "<if test='student_id != null'> and student_id=#{student_id}</if> " +
            "<if test='tc_id != null'> and tc_id=#{tc_id}</if> " +
            "limit #{offset}, #{limit}" +
            "</script>")
    @Results({
            @Result(column = "student_id", property = "student", javaType = Student.class,
                    one = @One(select="com.jlu155.CourseManager.repository.StudentRepository.findStudentById")),
            @Result(column = "course_id", property = "course", javaType = Course.class,
                    one = @One(select="com.jlu155.CourseManager.repository.CourseRepository.findCourseById")),
            @Result(column = "tc_id", property = "teachClass", javaType = TeachClass.class,
                    one = @One(select="com.jlu155.CourseManager.repository.TeachClassRepository.findTeachClassById")),
            @Result(column = "student_id", property = "student_id" ,javaType = Integer.class),
            @Result(column = "tc_id", property = "tc_id",javaType = Integer.class),
            @Result(column = "course_id", property = "course_id",javaType = Integer.class),
    })
    List<StudentCourse> findStudentCourseByConditions(
            @Param("student_id") Integer student_id,
            @Param("tc_id") Integer tc_id,
            @Param("offset") int offset,
            @Param("limit") int limit
    );

    // 按学生ID计数课程
    @Select("select count(*) from student_course where student_id=#{student_id}")
    Integer countCoursesBySid(Integer student_id);

    // 按课程ID查询学生信息
    @Select("select * from student_course where course_id=#{course_id}")
    List<StudentCourse> findStudentCourseByCourseId(Integer course_id);

    // 按教师课程ID查询学生课程信息
    @Select("select * from student_course where tc_id=#{tc_id}")
    List<StudentCourse> findStudentCourseByTcId(Integer tc_id);

    // 删除学生课程信息
    @Delete("delete from student_course where student_id=#{student_id} and tc_id=#{tc_id}")
    public boolean deleteStudentCourse( Integer student_id, Integer tc_id);

    // 查询所有学生课程信息
    @Select("select * from student_course")
    public List<StudentCourse> findAllStudentCourses();

    // 更新学生课程信息
    @Update("update student_course set grade=#{grade} where student_id=#{student_id} and course_id=#{course_id} and tc_id=#{tc_id}")
    public boolean updateStudentCourse(StudentCourse studentCourse);
}
